package cdu.zch.healthcare;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class BuyMedicineActivity extends AppCompatActivity {

    // 枚举药品信息
    private String[][] packages =
            {
                    {"Upnise-D3 1000IU 胶囊", "", "", "", "50"},
                    {"HealthVit Chromium Picolinate 200mcg 胶囊", "", "", "", "305"},
                    {"维生素 B 复合胶囊", "", "", "", "448"},
                    {"Inlife 维生素 E 小麦胚芽油胶囊", "", "", "", "539"},
                    {"Dol9650 片剂", "", "", "", "30"},
                    {"Crocin 650 速效片", "", "", "", "50"},
                    {"Strepsils 药用含片治疗喉咙痛", "", "", "", "40"},
                    {"Tata 1mg 钙 + 维生素 D3", "", "", "", "30"},
                    {"Eeronia -XT 片剂", "", "", "", "130"},
            };

    //药品的详细信息
    private String[] package_details = {
            "构建和保持骨骼和牙齿的强壮\n" +
                    "减少疲劳/压力和肌肉疼痛\n" +
                    "增强免疫力和提高对感染的抵抗力",

            "铬是一种必需的微量矿物质，在帮助胰岛素调节血糖方面起着重要作用。",

            "缓解维生素B缺乏症\n" +
                    "有助于红血球的形成\n" +
                    "保持健康的神经系统",

            "促进健康以及皮肤益处。\n" +
                    "有助于减少皮肤瑕疵和色素沉着。\n" +
                    "保护皮肤免受紫外线A和紫外线B的伤害。",

            "Dolo 650片剂通过阻止负责发热和疼痛的某些化学信使的释放来帮助缓解疼痛和发热。",

            "有助于缓解发热和降低高温\n" +
                    "适用于有心脏病或高血压的人",

            "缓解细菌性喉咙感染的症状，并舒缓恢复过程\n" +
                    "在喉咙痛时提供温暖舒适的感觉",

            "减少钙缺乏、佝偻病和骨质疏松的风险\n" +
                    "促进关节的活动性和灵活性",

            "有助于减少因慢性失血或铁摄入不足引起的铁缺乏"
    };

    HashMap<String, String> item;
    ArrayList list;
    SimpleAdapter sa;
    ListView lst;
    Button btnBack, btnGoToCart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_medicine);

        lst = findViewById(R.id.listViewBM);
        btnBack = findViewById(R.id.buttonBMBack);
        btnGoToCart = findViewById(R.id.buttonBMGoToCart);

        // 去购物车界面
        btnGoToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BuyMedicineActivity.this, CartBuyMedicineActivity.class));
            }
        });
        // 返回Home
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BuyMedicineActivity.this, HomeActivity.class));
            }
        });

        //初始化 ArrayList 并填充药品数据，每个药品的数据用 HashMap 存储。
        list = new ArrayList();
        for (int i = 0; i < packages.length; i++) {
            item = new HashMap<String, String>();
            item.put("line1", packages[i][0]);
            item.put("line2", packages[i][1]);
            item.put("line3", packages[i][2]);
            item.put("line4", packages[i][3]);
            item.put("line5", "合计: " + packages[i][4] + "/-");
            list.add(item);
        }

        //创建 SimpleAdapter 并将其设置为 ListView 的适配器，显示药品信息。
        sa = new SimpleAdapter(this, list,
                R.layout.multi_lines,
                new String[]{"line1", "line2", "line3", "line4", "line5"},
                new int[]{R.id.line_a, R.id.line_b, R.id.line_c, R.id.line_d, R.id.line_e});
        lst.setAdapter(sa);

        // 药品详情页面
        //设置 ListView 项目点击事件监听器，点击后跳转到 BuyMedicineDetailsActivity 活动，并传递药品名称、详情和价格。
        lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView,View view,int i,long l) {
                Intent it = new Intent(BuyMedicineActivity.this, BuyMedicineDetailsActivity.class);
                it.putExtra("text1" , packages[i][0]);
                it.putExtra("text2" , package_details[i]);
                it.putExtra("text3" , packages[i][4]);
                startActivity(it);
            }
        });
    }
}